import {
  http
} from '../../http/index';
var globalData = getApp().globalData;
Page({
  data: {
    postData: [],
    globalData
  },
  pre(event) {
    let { src, files } = event.currentTarget.dataset;
    let fileNameArray = files.split(',');
    const videoExtensions = ['mp4', 'avi', 'mov', 'mkv', 'wmv'];
    const filteredArray = fileNameArray.filter(item => {
      const fileExtension = item.split('.').pop().toLowerCase();
      return videoExtensions.indexOf(fileExtension) === -1;
    });
    const urls = filteredArray.map(fileName => this.data.globalData.URL + fileName);
    wx.previewImage({
      current: src, // 当前显示图片的http链接
      urls: urls // 需要预览的图片http链接列表
    })
  },
  // 查看详情
  goTeaDetail(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/postDetails/index' + '?id=' + id,
    });
  },

  // 发布帖子
  add() {
    let userInfo = wx.getStorageSync('userInfo');
    if (!userInfo.id) {
      wx.showToast({
        title: '请登录',
        icon: 'error'
      });
      return
    };
    wx.navigateTo({
      url: '/pages/addPost/index',
    });
  },
  // 获取数据
  getPost() {
    http({
      url: '/post/list',
    }).then(({ data }) => {
      this.setData({ postData: data.result });
    })
  },
    onShow() {
  this.getPost()
  }
})